﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;

namespace FaceFromCamera
{
    class RecognitionEngineControler
    {
        #region SingleTon
        static RecognitionEngineControler instance;
        public static RecognitionEngineControler Instance
        {
            get
            {
                if (instance == null)
                    instance = new RecognitionEngineControler();
                return instance;
            }
        }

        #endregion
        public delegate void ModelChangeDelegate(object sender, Person e);
        public event ModelChangeDelegate ModelChangeEvent;

        public void RegisterView(IView view)
        {
            this.ModelChangeEvent += new ModelChangeDelegate(view.ModelChange);
        }
        public void UnregisterView(IView view)
        {
            this.ModelChangeEvent -= new ModelChangeDelegate(view.ModelChange);
        }

        public void RaiseModelChange(object sender, Person e)
        {
            if (ModelChangeEvent != null)
                ModelChangeEvent(sender, e);
        }
        public Image UpdateFrame(Capture cap, HaarCascade haar)
        {
            return RecognitionEngineModel.Instance.Frame(cap, haar);
        }
        public Image GetFaceFromFrame()
        {
            return RecognitionEngineModel.Instance.faceOnly;
        }
        public Person Recognize()
        {


            FaceFromCamera.DatabaseModel.AllPhoto temp = DatabaseModel.Instance.DisplayImage();
            Image<Gray, byte>[] tablica = new Image<Gray, byte>[temp.photo.Count];
            for (int i = 0; i < temp.photo.Count; i++)
                tablica[i] = new Image<Gray, byte>(new Bitmap(temp.photo[i]));
            string[] id = new string[temp.photo.Count];
            for (int i = 0; i < temp.photo.Count; i++)
                id[i] = temp.id[i].ToString();
            string lab = RecognitionEngineModel.Instance.Recognize(tablica, id);
            Person osoba = DatabaseModel.Instance.GetUser(int.Parse(lab));
            return osoba;
        }





        public void ResetFace()
        {
            RecognitionEngineModel.Instance.faceOnly = null;
        }
    }
    
}
